
package Memmory;


public class Interpreter {
    public static Word fromString(String s) {
        String[] parts = s.split(" ");
        byte cmdCode = -1;
        
        switch(parts[0].toUpperCase()) {
            case "ADD":
                cmdCode = 1; 
                break;
            case "SUB":
                cmdCode = 2;
                break;
            case "MUL":
                cmdCode = 3;
                break;
            case "DIV":
                cmdCode = 4;
                break;
            case "CMP":
                cmdCode = 5;
                break;
            case "MOVAB":
                cmdCode = 6;
                break;
            case "MOVBA":
                cmdCode = 7;
                break;
            case "LOAD":
                cmdCode = 8;
                break;
            case "SET":
                cmdCode = 9;
                break;
            case "JMP":
                cmdCode = 10;
                break;
            case "GD":
                cmdCode = 11;
                break;
            case "PD":
                cmdCode = 12;
                break;
            case "HALT":
                cmdCode = 13;
                break;
            case "TOASCII":
                cmdCode = 14;
                break;
            case "JMPE":
                cmdCode = 15;
                break;
            case "ACTIVATEVM":
                cmdCode = 100;
                break;
            case "DROPVM":
                cmdCode = 101;
                break;
            case "CLEARSI":
                cmdCode = 102;
                break;
            case "CLEARTI":
                cmdCode = 103;
                break;
            case "CLEARPI":
                cmdCode = 104;
                break;
            case "CLEARIOI":
                cmdCode = 105;
                break;
            case "CLEARTIMER":
                cmdCode = 106;
                break;
            case "EXCHANGE":
                cmdCode = 107;
                break;
        }
        
        Word result;
        if(parts.length == 2) {
            String[] subParts = parts[1].split(",");
            result = new Word(0, cmdCode, Integer.parseInt(subParts[0]), Integer.parseInt(subParts[1]));
        } else {
            result = new Word(0, cmdCode, 0, 0);
        }
        
        return result;
    }
    
    public static String fromWord(Word w) {
        byte bytes[] = w.getBytes();
        
        switch(bytes[1]) {
            case 1:
                return "ADD";
            case 2:
                return "SUB";
            case 3:
                return "MUL";
            case 4:
                return "DIV";
            case 5:
                return "CMP";
            case 6:
                return "MOVAB";
            case 7:
                return "MOVBA";
            case 8:
                return "LOAD";
            case 9:
                return "SET";
            case 10:
                return "JMP";
            case 11:
                return "GD";
            case 12:
                return "PD";
            case 13:
                return "HALT";
            case 14:
                return "TOASCII";
            case 15:
                return "JMPE";
            case 100:
                return "ACTIVATEVM";
            case 101:
                return "DROPVM";
            case 102:
                return "CLEARSI";
            case 103:
                return "CLEARTI";
            case 104:
                return "CLEARPI";
            case 105:
                return "CLEARIOI";
            case 106:
                return "CLEARTIMER";
            case 107:
                return "EXCHANGE";
        }
        return "NONE";
    }
}